using System;
using System.IO.Packaging;
using System.Workflow.ComponentModel;
using System.Xml;
using System.IO;

namespace OWA.Activities
{
	public class ExtractCustomXmlActivity
        : AbstractCustomXmlActivity
	{
        protected override ActivityExecutionStatus Execute(
            ActivityExecutionContext executionContext)
        {
            Package package = GetPackage();
            PackagePart customXmlPart = GetCustomXmlPart(package, StoreItemID);
            if (customXmlPart == null)
            {
                throw new OWAException("Custom XML part not found.");
            }
            XmlDocument document = new XmlDocument();
            try
            {
                using (Stream stream = customXmlPart.GetStream(FileMode.Open, FileAccess.Read))
                {
                    document.Load(stream);
                }
            }
            catch (XmlException e)
            {
                throw new OWAException("Custom XML error");
            }
            CustomXml = document;
            return ActivityExecutionStatus.Closed;
        }
	}
}
